<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="renderer" content="webkit|ie-comp|ie-stand">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta http-equiv="Cache-Control" content="no-siteapp" />
    <title>数据库</title>
    <script src="../../../static/kaige/js/szload.js"></script>
</head>

<body>
    <div class="kaige-body layui-form">
        <table class="layui-table" id="datatable" lay-filter="datatable" lay-size="sm" style="margin-top: 0;"></table>
    </div>

    <script>
        load_init_js([
            szoapcpath + '/static/kaige/js/date.js'
        ], function () {
            layui.config({
                base: szoapcpath + '/static/kaige/layuiextend/'
            }).use(['element', 'jquery', 'layer', 'table'], function () {
                var element = layui.element, $ = layui.jquery, layer = layui.layer, table = layui.table;
                table.render({
                    elem: '#datatable'
                    , url: "/sys.dbase/datalist"
                    , id: 'datatableid'
                    , title: '系统数据库数据表'
                    , toolbar: '#toolbarDemo'
                    , height: 'full-25'
                    , defaultToolbar: ['filter']
                    , page: false
                    , cols: [[
                        { type: 'checkbox', fixed: 'left', unresize: true }
                        , { type: 'numbers', title: '序号', width: 60, fixed: 'left', unresize: true }
                        , { field: 'Name', title: '表名', sort: true, width: 240, fixed: 'left', unresize: true }
                        , { field: 'Rows', title: '记录数', sort: true, width: 100, align: 'right' }
                        , {
                            field: 'Data_length', title: '大小(kb)', sort: true, width: 120, align: 'right', templet: function (res) {
                                return (res.Data_length / 1024).toFixed(2)
                            }
                        }
                        , {
                            field: 'Data_free', sort: true, title: '冗余(kb)', width: 120, align: 'right', templet: function (res) {
                                return (res.Data_free / 1024).toFixed(2)
                            }
                        }
                        , { field: 'Engine', sort: true, title: '引擎', width: 120 }
                        , { field: 'Comment', sort: true, title: '备注', width: 200 }
                        , { field: 'Collation', sort: true, title: '编码', width: 180 }
                        , { field: 'Update_time', sort: true, title: '更新时间', width: 180 }
                        , { fixed: 'right', title: '操作', toolbar: '#barDemo', width: 110, fixed: 'right', unresize: true }
                    ]]
                });

                //头工具栏事件
                table.on('toolbar(datatable)', function (obj) {
                    var checkStatus = table.checkStatus(obj.config.id);
                    var data = checkStatus.data;
                    var ids = [];
                    if (0 >= data.length && 'data-export,data-optimize,data-repair'.indexOf(obj.event) != -1) {
                        layer.msg('请选择数据表');
                        return;
                    }
                    for (var i = 0; i < data.length; i++) {
                        ids.push(data[i].Name);
                    }
                    switch (obj.event) {
                        case 'data-import':
                            var iptindex = layer.open({
                                title: '<i class="fa fa-database"></i> 恢复数据库',
                                type: 1,
                                anim: 5,
                                area: ['980px', '550px'],
                                content: '<div style="padding:0 5px 0 5px"><table class="layui-hide" id="import_table" lay-filter="import_table"></table></div>'
                            });
                            table.render({
                                elem: '#import_table'
                                , url: "/sys.dbase/importlist"
                                , id: 'import_tableid'
                                , title: '已备份数据表'
                                , height: 470
                                , page: false
                                , cols: [[
                                    { type: 'numbers', title: '序号', width: 60, fixed: 'left', unresize: true }
                                    , {
                                        field: 'time', title: '备份名称', width: 200, fixed: 'left', unresize: true, templet: function (res) {
                                            return dateFtt("yyyyMMdd-hhmmss", new Date(res.time * 1000))
                                        }
                                    }
                                    , { field: 'part', title: '备份卷数', width: 100, align: 'right' }
                                    , { field: 'compress', title: '备份压缩', width: 120, align: 'right' }
                                    , {
                                        field: 'size', title: '大小(KB)', width: 120, align: 'right', templet: function (res) {
                                            return (res.size / 1024).toFixed(2)
                                        }
                                    }
                                    , {
                                        field: 'time', title: '备份时间', width: 180, templet: function (res) {
                                            return dateFtt("yyyy-MM-dd hh:mm:ss", new Date(res.time * 1000))
                                        }
                                    }
                                    , { fixed: 'right', title: '操作', toolbar: '#barimport', width: 120, fixed: 'right', unresize: true }
                                ]]
                            });
                            //监听行工具事件
                            table.on('tool(import_table)', function (obj) {
                                var data = obj.data;
                                if (obj.event === 'import') {
                                    layer.confirm('确定恢复吗？', { icon: 3, title: '名称：' + dateFtt('yyyyMMdd-hhmmss', new Date(data.time * 1000)) }, function (index) {
                                        var postindex = layer.load();
                                        $.post("/sys.dbase/import", { id: data.time }, function (res) {
                                            if (res.error == 0) {
                                                layer.msg(res.msg);
                                            } else {
                                                layer.msg(res.msg);
                                            }
                                            layer.close(postindex);
                                        });
                                        layer.close(index);
                                    });
                                } else if (obj.event === 'del') {
                                    layer.confirm('确定删除吗？', { icon: 3, title: '名称：' + dateFtt('yyyyMMdd-hhmmss', new Date(data.time * 1000)) }, function (index) {
                                        var postindex = layer.load();
                                        $.post("/sys.dbase/importdel", { id: data.time }, function (res) {
                                            if (res.error == 0) {
                                                layer.msg(res.msg);
                                                table.reload('import_tableid');
                                            } else {
                                                layer.msg(res.msg);
                                            }
                                            layer.close(postindex);
                                        });
                                        layer.close(index);
                                    });
                                }
                            });
                            break;
                        case 'data-export':
                            $.ajax({
                                url: "/sys.dbase/export",
                                data: { id: ids },
                                sign: 1,
                                type: 'POST',
                                success: function (res) {
                                    layer.msg(res.msg);
                                }
                            });
                            break;
                        case 'data-optimize':
                            $.ajax({
                                url: "/sys.dbase/optimize",
                                data: { id: ids },
                                sign: 1,
                                type: 'POST',
                                success: function (res) {
                                    layer.msg(res.msg);
                                    if (res.error == 0) {
                                        table.reload('datatableid');
                                    }
                                }
                            });
                            break;
                        case 'data-repair':
                            $.ajax({
                                url: "/sys.dbase/repair",
                                data: { id: ids },
                                sign: 1,
                                type: 'POST',
                                success: function (res) {
                                    layer.msg(res.msg);
                                    if (res.error == 0) {
                                        table.reload('datatableid');
                                    }
                                }
                            });
                            break;
                    }
                });

                //监听行工具事件
                table.on('tool(datatable)', function (obj) {
                    var data = obj.data;
                    if (obj.event === 'optimize') {
                        $.ajax({
                            url: "/sys.dbase/optimize",
                            data: { id: data.Name },
                            sign: 1,
                            type: 'POST',
                            success: function (res) {
                                layer.msg(res.msg);
                                if (res.error == 0) {
                                    table.reload('datatableid');
                                }
                            }
                        });
                    } else if (obj.event === 'repair') {
                        $.ajax({
                            url: "/sys.dbase/repair",
                            data: { id: data.Name },
                            sign: 1,
                            type: 'POST',
                            success: function (res) {
                                layer.msg(res.msg);
                                if (res.error == 0) {
                                    table.reload('datatableid');
                                }
                            }
                        });
                    }
                });

            });
        });
    </script>
    <script type="text/html" id="toolbarDemo">
        <div class="layui-btn-group">
            <button class="layui-btn layui-btn-sm layui-btn-normal" lay-event="data-export"><i class="fa fa-copy"></i> 备份数据库</button>
            <button class="layui-btn layui-btn-sm layui-btn-danger" lay-event="data-import"><i class="fa fa-database"></i> 恢复数据库</button>
        </div>
        <div class="layui-btn-group">
            <button class="layui-btn layui-btn-sm" lay-event="data-optimize"><i class="fa fa-server"></i> 优化数据库</button>
            <button class="layui-btn layui-btn-sm" lay-event="data-repair"><i class="fa fa-repeat"></i> 修复数据库</button>
        </div>
    </script>

    <script type="text/html" id="barDemo">
        <div class="layui-btn-group">
            <button class="layui-btn layui-btn-sm" lay-event="optimize" title="优化"><i class="fa fa-server"></i></button>
            <button class="layui-btn layui-btn-sm" lay-event="repair" title="修复"><i class="fa fa-repeat"></i></button>
        </div>
    </script>

    <script type="text/html" id="barimport">
        <div class="layui-btn-group">
            <button class="layui-btn layui-btn-sm" lay-event="import" title="恢复"><i class="fa fa-database"></i></button>
            <button class="layui-btn layui-btn-sm" lay-event="del" title="删除"><i class="fa fa-trash-o"></i></button>
        </div>
    </script>
</body>

</html>